//
//  SceneCell.m
//  YSSmartHome
//
//  Created by Yahaong on 16/9/6.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "SceneCell.h"
#import "MQTTSessionManager.h"
#import "MQTTCoverTool.h"
#import "ZTLinkageUtil.h"
#import "SceneViewController.h"
#import "CellBottomView.h"

@interface SceneCell ()
<
CellBottomViewDelegate
>

@property(nonatomic, strong)UIImageView *imageView;
@property(nonatomic, strong)UILabel *textLabel;
@property(nonatomic, strong)UIView *topView;
@property(nonatomic, strong)CellBottomView *bottomView;

@end

@implementation SceneCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initSubviews];
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)setIsEditOrDel:(BOOL)isEditOrDel
{
    if (isEditOrDel)
    {
        [self showBottomView];
    }
    else
    {
        [self cellRecover];
    }
}

- (void)setLinkage:(ZTLinkage *)linkage
{
    _linkage = linkage;
    NSArray *arrayName = [linkage.name componentsSeparatedByString:@"."];
    if (arrayName.count != 2) {
        debugLogInfo(@"场景名字不对！ %@",linkage.name);
        self.imageView.image = nil;
        self.textLabel.text = nil;
        return;
    }
    self.textLabel.text = arrayName[0];
    if (linkage.num != self.currentLinkmum) {
        NSString *strIcon = [NSString stringWithFormat:@"scene_disable_icon%@",arrayName[1]];
        self.imageView.image = [UIImage imageNamed:strIcon];
    }else
    {
        NSString *strIcon = [NSString stringWithFormat:@"scene_select_icon%@",arrayName[1]];
        self.imageView.image = [UIImage imageNamed:strIcon];
    }
}
- (void)showBottomView
{
    [self.bottomView updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(kIphoneSize_Height(30));
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
    
}
- (void)cellRecover
{
    [self.bottomView updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(0);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}


#pragma mark - CellBottomViewDelegate
- (void)cellottomView:(CellBottomView* __nonnull)view clickType:(CellBottomViewClickType)clickType
{
    [self cellRecover];
    if (clickType == CellBottomViewClickTypeEdit)
    {
        if (self.block) {
            self.block(SceneCellBtnFlagEdit);
        }
    }
    else
    {
        [ZTLinkageUtil delLinkage:self.linkage];
        if (self.block) {
            self.block(SceneCellBtnFlagDel);
        }
    }
}

#pragma mark - private method 
- (void)initSubviews
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bottomView];
    [self.contentView addSubview:self.topView];
    [self.topView addSubview:self.imageView];
    [self.topView addSubview:self.textLabel];
    [self layoutPageSubviews];
}

- (void)layoutPageSubviews
{
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(0);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.equalTo(self.contentView);
        make.height.equalTo(self.contentView);
        make.bottom.equalTo(self.bottomView.top);
    }];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
        make.width.height.equalTo(75);
    }];
    
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topView);
        make.top.equalTo(self.imageView.bottom);
    }];
}


#pragma mark - getters and setters
- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView =[[ UIImageView alloc]init];
    }
    return _imageView;
}
- (UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]init];
        _textLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _textLabel;
}

- (CellBottomView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[CellBottomView alloc]init];
        _bottomView.delegate = self;
    }
    return _bottomView;
}
- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc]init];
    }
    return _topView;
}
@end
